package com.xhxi.photobooker.service.impl;

import com.xhxi.photobooker.entity.Photographer;
import com.xhxi.photobooker.mapper.PhotographerMapper;
import com.xhxi.photobooker.service.OrderService;
import com.xhxi.photobooker.service.PhotographerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.concurrent.TimeUnit;

@Service
public class PhotographerServiceImpl implements PhotographerService {

    @Autowired
    private PhotographerMapper photographerMapper;
    
    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Photographer savePhotographer(Photographer photographer) {
        photographerMapper.insert(photographer);
        return photographer;
    }

    @Override
    public Photographer findPhotographerById(Long id) {
        String cacheKey = "photographer:detail:" + id;
        Photographer photographer = (Photographer) redisTemplate.opsForValue().get(cacheKey);
        if (photographer != null) {
            return photographer;
        }
        photographer = photographerMapper.selectById(id);
        if (photographer != null) {
            // 动态计算完成订单数
            Integer completedCount = orderService.getCompletedOrderCount(id);
            photographer.setOrderCount(completedCount != null ? completedCount : 0);
            // 写入缓存，设置过期时间1小时
            redisTemplate.opsForValue().set(cacheKey, photographer, 1, TimeUnit.HOURS);
        }
        return photographer;
    }

    @Override
    public List<Photographer> findAllPhotographer() {
        List<Photographer> photographers = photographerMapper.selectList(null);
        // 为每个摄影师动态计算完成订单数
        for (Photographer photographer : photographers) {
            Integer completedCount = orderService.getCompletedOrderCount(photographer.getId());
            photographer.setOrderCount(completedCount != null ? completedCount : 0);
        }
        return photographers;
    }

    @Override
    public Photographer updatePhotographer(Long id, Photographer photographer) {
        //判断是否存在该对象
        Photographer photographer1 = photographerMapper.selectById(id);
        if(photographer1!=null)
        {
            photographerMapper.updateById(photographer);
        }
        return null;
    }

    @Override
    public boolean deletePhotographer(Long id) {
        int result = photographerMapper.deleteById(id);
        return result>0;
    }

    @Override
    public Photographer findByUserId(Long userId) {
        QueryWrapper<Photographer> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        Photographer photographer = photographerMapper.selectOne(wrapper);
        if (photographer != null) {
            // 动态计算完成订单数
            Integer completedCount = orderService.getCompletedOrderCount(photographer.getId());
            photographer.setOrderCount(completedCount != null ? completedCount : 0);
        }
        return photographer;
    }
    
    @Override
    public void updateOrderCount(Long photographerId) {
        Integer completedCount = orderService.getCompletedOrderCount(photographerId);
        Photographer photographer = photographerMapper.selectById(photographerId);
        if (photographer != null) {
            photographer.setOrderCount(completedCount != null ? completedCount : 0);
            photographerMapper.updateById(photographer);
        }
    }

    @Override
    public List<Photographer> getAllPhotographers() {
        return photographerMapper.selectList(null);
    }
}
